class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int,int> hash;
        int ret=0;
        int sum=0;
        hash[0] = -1;
        for(int i=0;i<nums.size();++i)
        {
            if(nums[i]==0)
            {
                sum-=1;
            }
            else if(nums[i]==1)
            {
                sum+=1;
            }
            if(hash.find(sum) != hash.end())
            {
                ret=max(ret,i-hash[sum]);
            }
            if(hash.find(sum) == hash.end())
            {
                 hash[sum]=i;
            }
        }
        return ret;
    }
};
